<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>添加合伙人</title>
<meta name="viewport" content="width=100%, initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="/lib/weix/style/weui.min.css">
<link rel="stylesheet" href="/css/fonts.css">
<link rel="stylesheet" href="/css/comm.css">
<link rel="stylesheet" href="/css/form_hej.css">
<link rel="stylesheet" href="/css/form_hehr.css">
</head>

<body>
<div class="tanj-hhr__box" id="J_qie_box">
    <a class="on" href="javascript:;"><span class="tanj-hhr__quan">1</span><span>填写信息</span></a>
    <a class="th-xx_left" href="javascript:;"><span class="tanj-hhr__quan">2</span><span>公众号登录</span></a>
</div>

<div class="J_qie_nrt">
    <div class="weui-cells weui-cells_form tbd" style="margin-top:-2px">
        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label"><i class="icon-user "></i></label></div>
            <div class="weui-cell__bd">
                <input class="weui-input" type="text" placeholder="姓名" id="J_name">
            </div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label"><i class="icon-phone"></i></label></div>
            <div class="weui-cell__bd">
                <input class="weui-input" type="tel" placeholder="手机" id="J_phone"  maxlength="11">
            </div>
        </div>
        <div class="weui-cell weui-cell_select weui-cell_select-after" id="J_role_b">
            <div class="weui-cell__hd">
                <label for="" class="weui-label"><i class="icon-necktie"></i></label>
            </div>
            <div class="weui-cell__bd select">
                <select class="weui-select" id="J_role" selected="true">
                    <option selected="selected" disabled="disabled"  style="display:none" value="">类型</option>
                </select>
                <div class="xia"><i class="icon-arrow"></i></div>
            </div>
            <div class="weui-cell__ft" id="J_rolemore" style="display:none;">
                <a href="gl_jues.html" class="weui-vcode-btn jia"><i class="icon-plus"></i></a>
            </div>
        </div>
    </div>
    
    <div class="weui-cells weui-cells_form tbd" id="J_group_box" style="display:none;">
        <div class="weui-cell weui-cell_select weui-cell_select-after">
            <div class="weui-cell__hd">
                <label for="" class="weui-label"><i class="icon-folder"></i></label>
            </div>
            <div class="weui-cell__bd select">
                <select class="weui-select" id="J_group" selected="true">
                    <option selected="selected" disabled="disabled"  style="display:none" value="">分组</option>
                </select>
                <div class="xia"><i class="icon-arrow"></i></div>
            </div>
            <div class="weui-cell__ft" id="J_groupmore" style="display:none;">
                <a href="/userPage/user_role/2" class="weui-vcode-btn jia"><i class="icon-plus"></i></a>	<!--2员工、3合伙人-->
            </div>
        </div>
    </div>
	
    <input type="hidden" id="J_pid" value = "${uid}">
    <div class="weui-btn-area">
        <a class="weui-btn weui-btn_primary" href="javascript:" id="J_submit">添加</a>
    </div>
</div>

<div class="J_qie_nrt" style="display:none;">
	<div class="th-xx_top tanj-hhr__saom" style="margin-top:-2px">
		<p class="p1" style="padding-top:20px;">请<span class="J_jues">合伙人</span>微信扫码，关注公众号并登录</p>
        <p class="p2"><span id="J_name_2">-</span>&nbsp;/&nbsp;<span id="J_phone_2">-</span>&nbsp;/&nbsp;<span id="J_role_name_2">-</span></p>
        <div class="tanj-hhr__saom-mabox" style="padding:0;"><img src="" id="J_pic" /></div>
    </div>
</div>


<script src="/js/zepto.min.js"></script>
<script src="https://res.wx.qq.com/open/libs/weuijs/1.0.0/weui.min.js"></script>
<script src="/js/comm.js"></script>
<script type="text/javascript">
//权限
function quanx_csh(topen){
	var uid = "${uid}";
	var data = {uid:uid};
	var url = "/permission/mypermission";
	$.ajax({
		type: "POST",
		async: false,
		dataType: 'json',
		contentType: "application/json; charset=utf-8",
		data: JSON.stringify(data),
		url: url,
		success: function(res){
			if(res.code == 200){
				var datas = res.data;
				
				for (var i = 0;i<datas.length;i++){
					var code_data = datas[i].code
					//console.log(code_data)
					//code 10001员工管理 10002客户管理	10003设置管理 10004报备管理 10005积分管理 10006开单管理 10007任务管理 10008合伙人管理 10009添加合伙人  10010客户报备 10011优惠开单 10012做任务
					
					if(code_data == 10001){
						if(topen == 2){	//topen = 2 添加员工   topen = 3 添加合伙人
							//$('#J_rolemore').remove()
							//$('#J_groupmore').remove()
						}	
					}
				}
				
			}else{
				weui.topTips(res.msg);
			}
		},
		error: function(){

		},
		complete: function(){
		}
	});
}

//类型
function getRolelist(id){
	var data = {id:id,uid:"${uid}"};
	var url = "/role/getRole";
	$.ajax({
		type: "POST",
		async: false,
		dataType: 'json',
		contentType: "application/json; charset=utf-8",
		data: JSON.stringify(data),
		url: url,
		success: function(res){
		 if(res.code == 200){
			var datas = res.data.list;
			var shtml = '';
			for (var i =0;i<datas.length;i++){
				shtml += '<option value="'+datas[i]['id']+'">'+datas[i]['name']+'</option>'
			}
		  $('#J_role').append(shtml);
		 }else{
			weui.topTips(res.msg)
		 }
		},
		error: function(){

		},
		complete: function(){
		}
	});
}

//分组
function getGrouplist(id){
	var data = {id:id}; //id 1，员工  2，合伙人    
	var url = "/group/group_list";
	$.ajax({
		type: "POST",
		async: false,
		dataType: 'json',
		contentType: "application/json; charset=utf-8",
		data: JSON.stringify(data),
		url: url,
		success: function(res){
			if(res.code == 200){
				var datas = res.data.list;
				var shtml = '';
				for (var i =0;i<datas.length;i++){
					shtml += "<option value='"+datas[i]['id']+"'>"+datas[i]['name']+"</option>"
				}
				$('#J_group').append(shtml);
			}else{
				weui.topTips(res.msg)
			}
		},
		error: function(){

		},
		complete: function(){
		}
	});
}

//获取二维码
function code(id){
	var data = {id:id};
	var url = "/user/user_info";
	$.ajax({
		type: "POST",
		async: false,
		dataType: 'json',
		contentType: "application/json; charset=utf-8",
		data: JSON.stringify(data),
		url: url,
		success: function(res){
			if(res.code == 200){
				var datas = res.data;
				$("#J_name_2").html(datas.name);
				$("#J_phone_2").html(datas.phone);
				$("#J_role_name_2").html(datas.roleName);
				$("#J_pic").attr('src',datas.qrcode)					
			}else{
				weui.topTips(res.msg)
			}
		},
		error: function(){

		},
		complete: function(){
		}
	});
}


$(function(){		
	var topen = GetQueryString('topen');   //topen = 2 添加员工   topen = 3 添加合伙人
	
	if(topen == 2){
		$('title').html('添加员工')
		$('#J_jues').html('员工')
		//quanx_csh(2) //权限判断
	}else if(topen == 3){
		$('title').html('添加合伙人')
		$('#J_jues').html('合伙人')
		//quanx_csh(3) //权限判断
	}else{
		$('title').html('未知操作')
	}
	
	var user_id = GetQueryString('id');	//传入id则为再次绑定，直接跳转二维码
	//-----------------------------------------------------------------------
	//返回本页刷新
	window.addEventListener("pageshow", function(){
		if(sessionStorage.getItem("need-refresh")){
			location.reload();
			sessionStorage.removeItem("need-refresh");
		}
	});
	//-----------------------------------------------------------------------
	//初始化类型和组
	getRolelist(topen); 			//类型
	if(topen == 2){
		getGrouplist(1); 			//分组数据
		$('#J_group_box').show() 	//显示分组
	}
	
	if(user_id){
		code(user_id)
		$('#J_qie_box a').eq(0).removeClass('on')
		$('#J_qie_box a').eq(1).addClass('on')
		$('.J_qie_nrt').eq(0).hide()
		$('.J_qie_nrt').eq(1).show()
	}
	//-----------------------------------------------------------------------
	//提交操作
	$(document).on('click','#J_submit',function(){
		var name_data = $('#J_name').val().trim();
		var tel_data = $('#J_phone').val().trim();
		var leix_data = $('#J_role').val();
		var group_data = $('#J_group').val();
		
		if(!name(name_data)){
			return false
		}

		if(!tel(tel_data)){
			return false
		}

		if(leix_data == ''){
			weui.topTips('请选择类型');
			return false		
		}
		
		var name_dd = ''	//名称
		var zhu_data = 100  //分组
		
		if(topen == 2){
			name_dd = '员工'
			zhu_data = $('#J_group').val()
		}else if(topen == 3){
			name_dd = '合伙人'
			zhu_data = 2
		}
		
		weui.dialog({
			title: '确定添加',
			content: '是否确定添加该'+name_dd+'？',
			className: 'custom-classname',
			buttons: [{
				label: '取消',
				type: 'default',
				onClick: function () {}
			}, {
				label: '确定',
				type: 'primary',
				onClick: function () {
					 
					var loading = weui.loading('数据提交中', {
						className: 'custom-classname'
					});
					 
					setTimeout(function () {
						loading.hide();

						
						var data = {pid:$("#J_pid").val(),
									name:$("#J_name").val(),
									phone:$("#J_phone").val(),
									role_id:$("#J_role").val(),
									group_id:zhu_data};
						
						var url = "/user/editUser";
						$.ajax({
							type: "POST",
							dataType: 'json',
							async: false,
							contentType: "application/json; charset=utf-8",
							data: JSON.stringify(data),
							url: url,
							success: function(res){
								if(res.code == 200){
									weui.toast('添加成功', {
										duration: 1000,
										className: 'custom-classname',
										callback: function(){ 
										console.log(res.data,"111");
											code(res.data)
											$('#J_qie_box a').eq(0).removeClass('on')
											$('#J_qie_box a').eq(1).addClass('on')
											$('.J_qie_nrt').eq(0).hide()
											$('.J_qie_nrt').eq(1).show()
											//location.replace('')
										}
									});
								}else{
									weui.topTips(res.msg)
								}
							},
							error: function(){
			
							},
							complete: function(){
							}
						});
						
					}, 1000);
					 
				}
			}]
		});
		
		

	})
		
});
</script>
</body>
</html>